from django.db import models

from apps.user.models import User

REMARK = "“01”代表实践课，“02”代表公共必修课，“03”代表专业基础课，“04”代表专业核心课，“05”代表专业选修课，“06”代表公共选修课"


class Scholarship(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    name = models.CharField(db_column='name', max_length=20, verbose_name='奖学金名称')

    def __str__(self):
        return '%s' % self.name

    class Meta:
        db_table = 'scholarship'
        verbose_name_plural = '奖学金名称'


class Type(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    name = models.CharField(db_column='name', max_length=20)

    def __str__(self):
        return '%s' % self.name

    class Meta:
        db_table = 'type'
        verbose_name_plural = '类型'


class Additional(models.Model):
    NATURE_CHOICES = (
        ('0', '扣分'),
        ('1', '加分')
    )
    id = models.AutoField(db_column='id', primary_key=True)
    name = models.CharField(db_column='name', max_length=50)
    fraction = models.CharField(db_column='fraction', max_length=10, verbose_name='分数', null=True, blank=True)
    type = models.ForeignKey(Type, models.DO_NOTHING, db_column='type')
    nature = models.CharField(
        max_length=1, choices=NATURE_CHOICES,
        verbose_name='性质', default='1'
    )
    scholarship_id = models.ForeignKey(Scholarship, models.DO_NOTHING, db_column='scholarship_id', verbose_name='奖学金名称',
                                       default=1, related_name="scholarship_additional")

    def __str__(self):
        return '%s' % self.name

    class Meta:
        db_table = 'additional'
        verbose_name_plural = '附加分'


class Rank(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    name = models.CharField(db_column='name', max_length=20)
    fraction = models.CharField(db_column='fraction', max_length=10, verbose_name='分数', null=True, blank=True)
    additional_id = models.ForeignKey(Additional, models.DO_NOTHING, db_column='additional_id',
                                      related_name="additional_rank")

    class Meta:
        db_table = 'rank'
        verbose_name_plural = '附加分等级'


class Grade(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    grade = models.CharField(db_column='grade', max_length=10, verbose_name='成绩')
    course_name = models.CharField(db_column='course_name', max_length=30, verbose_name='课程名称')
    credit = models.FloatField(db_column='credit', max_length=5, verbose_name='学分')
    user_id = models.ForeignKey(User, models.DO_NOTHING, db_column='user_id', verbose_name='学生id',
                                related_name='user_grade')
    nature = models.CharField(db_column='nature', max_length=4, verbose_name='课程性质', null=True, help_text=REMARK)

    class Meta:
        db_table = 'grade'
        verbose_name_plural = '成绩表'


class SubmitRecords(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    user_id = models.ForeignKey(User, models.DO_NOTHING, db_column='user_id', verbose_name='学生id',
                                related_name='user_records')
    semester = models.CharField(db_column='semester', max_length=5, verbose_name='学期', null=True)
    grade = models.TextField(db_column='grade_list', verbose_name='成绩记录')
    character = models.TextField(db_column='character_list', verbose_name='品德记录', null=True)
    study = models.TextField(db_column='study_list', verbose_name='学业记录', null=True)
    style = models.TextField(db_column='style_list', verbose_name='文体记录', null=True)
    other = models.TextField(db_column='other_list', verbose_name='其它记录', null=True)
    character_result = models.FloatField(db_column='character_result', verbose_name='品德成绩', null=True)
    study_result = models.FloatField(db_column='study_result', verbose_name='学业成绩', null=True)
    style_result = models.FloatField(db_column='style_result', verbose_name='文体成绩', null=True)
    course_results = models.FloatField(db_column='course_results', verbose_name='课程成绩', null=True)
    total_score = models.FloatField(db_column='total_score', verbose_name='总成绩', null=True, blank=True)
    scholarship_id = models.ForeignKey(Scholarship, models.DO_NOTHING, db_column='scholarship_id', verbose_name='奖学金名称',
                                       default=1, related_name="scholarship_records")

    class Meta:
        db_table = 'submit_records'
        verbose_name_plural = '奖学金申请记录'
        unique_together = ('semester', 'user_id')
